<template>
  <div>
    <!--面包屑导航-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--card卡片-->
    <el-card>
      <!--搜索-->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input v-model="goodsQuery.query" placeholder="请输入内容"
                    clearable @clear="getGoods" @blur="getGoods">
            <el-button slot="append" icon="el-icon-search" @click="getGoods"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!--table表格-->
      <el-table :data="goodsTableData" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="90px"></el-table-column>
        <el-table-column label="创建时间" width="160px">
          <template slot-scope="scope">
            {{scope.row.upd_time | myDate}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" type="primary" size="mini"></el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="del(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="goodsQuery.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="goodsQuery.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        // table表格
        goodsTableData: [],
        goodsQuery: {
          query: '',
          pagenum: 1,
          pagesize: 10
        },
        total: 0
      }
    },
    created () {
      this.getGoods()
    },
    methods: {
      // 获取商品列表数据
      getGoods: async function () {
        const { data: res } = await this.$http.get('goods', { params: this.goodsQuery })
        console.log(res)
        if (res.meta.status !== 200) return this.$message.error('数据获取失败！')
        this.$message.success('数据获取成功！')
        this.goodsTableData = res.data.goods
        this.total = res.data.total
      },
      // 分页每页条数
      handleSizeChange: function (val) {
        this.goodsQuery.pagesize = val
        this.getGoods()
      },
      // 分页当前页
      handleCurrentChange: function (val) {
        this.goodsQuery.pagenum = val
        this.getGoods()
      },
      // 删除商品
      del: function (row) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const { data: res } = await this.$http.delete(`goods/${row.goods_id}`)
          if (res.meta.status !== 200) return this.$message.error('删除商品失败！')
          this.$message.success('删除商品成功！')
          this.getGoods()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      goAddPage: function () {
        this.$router.push('/goods/add')
      }
    }
  }
</script>

<style lang="less" scoped></style>
